package modules

import (
	"fmt"
	"github.com/jordan-wright/email"
	"gspeed/bootstrap/global"
	"net/smtp"
)

type emailGo struct {
	pool *email.Pool
}

var Email emailGo

func (e *emailGo) New() *email.Pool {
	// 设置默认值
	global.Config.SetDefault("email.SMTPHost", "")
	global.Config.SetDefault("email.SMTPPort", "")
	global.Config.SetDefault("email.MaxClient", 5)
	global.Config.SetDefault("email.Usernme", "")
	global.Config.SetDefault("email.Password", "")
	var err error
	if e.pool == nil {
		// 创建连接池
		e.pool, err = email.NewPool(
			global.Config.GetString("email.SMTPHost")+":"+global.Config.GetString("email.SMTPPort"),
			global.Config.GetInt("email.MaxClient"),
			smtp.PlainAuth(
				"",
				global.Config.GetString("email.Username"),
				global.Config.GetString("email.Password"),
				global.Config.GetString("email.SMTPHost")),
		)
		if err != nil {
			fmt.Println("连接池初始错误")
			global.Logger.Fatal(err.Error())
		}
	}
	return e.pool
}
